﻿Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.ServiceProcess
Imports Microsoft.Reporting.WinForms
Imports System.Net.Mail
Imports System.IO.Packaging
Imports System
Imports System.Text
Imports System.Web.Services.Protocols
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Net
Imports System.Service

Public Class Functions
    
    
    Public Shared Sub WriteLog(ByVal Str As String)
        Dim oWrite As IO.StreamWriter
        Dim FilePath As String
        FilePath = Application.StartupPath + "\logfile.txt"

        If IO.File.Exists(FilePath) Then
            oWrite = IO.File.AppendText(FilePath)
        Else
            oWrite = IO.File.CreateText(FilePath)
        End If
        oWrite.Write(Now.ToString() + ":" + Str + vbCrLf)
        oWrite.Close()
    End Sub
    
    Public Shared Sub WriteXMLLog(DocType As String, XMLStr As String, ErrMsg As String)
        Dim cn As New Connection
        If XMLStr.Contains("'") Then
            XMLStr = XMLStr.Replace(",", "''")
        End If
        If ErrMsg.Contains("'") Then
            ErrMsg = ErrMsg.Replace(",", "''")
        End If
        cn.Integration_RunQuery("sp_XMLLog_Insert '" + DocType + "','" + XMLStr + "','" + ErrMsg + "'")
    End Sub
    Public Sub AutoRun()
        Try

      
        Dim MyArr As Array
        Dim Str As String
        ' Str = System.Configuration.ConfigurationSettings.AppSettings.Get("strConnect")
        Dim sCon As String = ""
        Dim strConnect As String = "Country"
        sCon = System.Configuration.ConfigurationSettings.AppSettings.Get(strConnect)
        MyArr = sCon.Split(";")
        PublicVariable.CountryCode = MyArr(0).ToString()
            Functions.WriteLog("AutoRun:" & PublicVariable.CountryCode)
        Dim myService As New electra_pc.ServiceSoapClient
        Dim ds As New DataSet()

        Dim myreport As New electra_pc.ServiceSoapClient

        Dim dt As DataTable = myreport.GetDBInfo(PublicVariable.CountryCode)
            Functions.WriteLog("AutoRun:" & dt.Rows(0)(0))

        ' Dim st As String = myService.GetDBInfo("SG")
        ' Dim xmlnode As XmlDocument = myService.GetDBInfo("SG")

        Dim connect As New Connection()
        Dim imp As New oImport

        Dim i As Integer = 0
        Dim Status As String = ""
        'Dim xm As New oXML
        'xm.SetDB()
        '  Button4.Enabled = False
        'ByVal SQLDBN As String, ByVal SAPUserN As String, ByVal SAPPwdN As String, ByVal SQLServerN As String, 
        'ByVal SQLUserN As String, ByVal SQLPwdN As String, ByVal SAPLicenseServerN As String, ByVal SQLTypeN As String)
        For i = 0 To dt.Rows.Count - 1
                If Connection.bConnect = False Then
                    Functions.WriteLog("AutoRun:ForLoop" & dt.Rows(0)(0))
                    setDB1(dt.Rows(i)(0), dt.Rows(i)(4), dt.Rows(i)(5), dt.Rows(i)(10), dt.Rows(i)(6), dt.Rows(i)(7), dt.Rows(i)(11), dt.Rows(i)(8))
                    Status = connectDB()
                    If Status <> "" Then
                        Functions.WriteLog("AutoRun:ForLoop" & Status)
                        Exit Sub
                    Else
                        Functions.WriteLog("AutoRun:Connection Okey")
                        imp.Import()
                        Connection.bConnect = False
                        PublicVariable.oCompany.Disconnect()
                    End If
                Else
                    imp.Import()
                    Connection.bConnect = False
                    PublicVariable.oCompany.Disconnect()
                End If


        Next
        Catch ex As Exception
            Functions.WriteLog("AutoRun:" & ex.Message)
        End Try
        'Try
        '    PublicVariable.AutoRetry = CBool(System.Configuration.ConfigurationSettings.AppSettings.Get("AutoRetry"))
        'Catch ex As Exception
        '    WriteLog("AutoRun+set autoretry:" + ex.ToString)
        'End Try

        'If PublicVariable.AutoRetry Then
        '    Dim a As New oAutoRetry
        '    a.RetryAll()
        'End If

        ''------------RECEIVE GRPO--------------
        'Dim orp As New oGRPO
        'orp.CreateGRPO()

        ''------------RECEIVE TRANFER--------------
        'Dim otr As New oTransfer
        'otr.CreateTransfer()

        ''------------RECEIVE INVOICE--------------
        'Dim oin As New oInvoice
        'oin.CreateInvoice()

        ''------------RECEIVE Goods Return--------------
        'Dim ort As New oReturn
        'ort.CreateGoodsReturn()

        ''------------SEND EMAIL-------------------
        'Dim oem As New oEmailWeb
        'oem.SendPOEmail()



    End Sub

    Public Sub setDB1(ByVal SQLDBN As String, ByVal SAPUserN As String, ByVal SAPPwdN As String, ByVal SQLServerN As String, ByVal SQLUserN As String, ByVal SQLPwdN As String, ByVal SAPLicenseServerN As String, ByVal SQLTypeN As String)
        Try
            Dim strConnect As String = ""
            Dim sCon As String = ""
            Dim sErrMsg As String = ""
            PublicVariable.SQLDB = SQLDBN
            PublicVariable.SAPUser = SAPUserN
            PublicVariable.SAPPwd = SAPPwdN
            PublicVariable.SQLServer = SQLServerN
            PublicVariable.SQLUser = SQLUserN
            PublicVariable.SQLPwd = SQLPwdN
            PublicVariable.SAPLicenseServer = SAPLicenseServerN
            PublicVariable.SQLType = SQLTypeN
            'PublicVariable.CountryCode = objIniFile.GetString("SAP", "CountryCode", "")
            'PublicVariable.Series = objIniFile.GetString("SAP", "ItemSeries", "")
            If IsNothing(PublicVariable.oCompany) Then
                PublicVariable.oCompany = New SAPbobsCOM.Company
            End If
            PublicVariable.oCompany.CompanyDB = PublicVariable.SQLDB ' MyArr(0).ToString.Trim()
            PublicVariable.oCompany.UserName = PublicVariable.SAPUser ' MyArr(1).ToString.trim()
            PublicVariable.oCompany.Password = PublicVariable.SAPPwd ' MyArr(2).ToString.trim()
            PublicVariable.oCompany.Server = PublicVariable.SQLServer ' MyArr(3).ToString.trim()
            PublicVariable.oCompany.DbUserName = PublicVariable.SQLUser ' MyArr(4).ToString.trim()
            PublicVariable.oCompany.DbPassword = PublicVariable.SQLPwd ' MyArr(5).ToString.Trim()
            PublicVariable.oCompany.LicenseServer = PublicVariable.SAPLicenseServer ' MyArr(6)
            'SQLType = PublicVariable.SQLType ' MyArr(7)
            If PublicVariable.SQLType = 2008 Then
                PublicVariable.oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008
            ElseIf PublicVariable.SQLType = 2005 Then
                PublicVariable.oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2005
            Else
                PublicVariable.oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2012
            End If

        Catch ex As Exception
            Functions.WriteLog("setDB1" & ex.ToString)
        End Try
    End Sub
    Public Function connectDB() As String
        Try
            Dim sErrMsg As String = ""
            Dim connectOk As Integer = 0
            If PublicVariable.oCompany.Connect() <> 0 Then
                PublicVariable.oCompany.GetLastError(connectOk, sErrMsg)
                Connection.bConnect = False
                Functions.WriteLog("Error Msg:" & sErrMsg)
                Return sErrMsg
            Else
                Connection.bConnect = True
                Return sErrMsg
            End If
        Catch ex As Exception
            Return False
            Functions.WriteLog("connectDB:" & ex.ToString)
        End Try
    End Function

End Class
